package com.cea.papyrus.core.listener;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Property;


/**
 * This class is a listener on GraphElement. It takes in account the type of element and refresh the concerned element.
 */
public class GraphElementListener implements Adapter {

	/** The _target. */
	protected Notifier _target;

	/** The channel. */
	protected static int channel=301;

	/** The edit part. */
	protected AbstractGraphicalEditPart editPart;

	/**
	 * this class create a listener on the umlElement.
	 * 
	 * @param editPart the editpart of the UMLElement
	 */
	public GraphElementListener(AbstractGraphicalEditPart editPart){

		this.editPart=(AbstractGraphicalEditPart)editPart;

	}

	/**
	 * Notify changed.
	 * 
	 * @param notification the notification
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 */
	public void notifyChanged(Notification notification) {
		// EditPart parent =umlEditPart.getParent();
		Debug.debug(this, "Notification GE = "+notification, channel);
		// Refresh the element
		if (notification.getEventType() != Notification.REMOVING_ADAPTER) {
			editPart.refresh();	

			// If this is a label of a link we have to refresh the parent
			if (editPart.getParent() instanceof GraphEdgeEditPart) {
				editPart.getParent().refresh();
			}

			// Default case
			// @TODO Bizarre, on a deja teste ce type d'event !!!
			if (notification.getEventType() != Notification.REMOVING_ADAPTER) {
				if (editPart instanceof GraphEdgeEditPart){
					((GraphEdgeEditPart) editPart).getModelManager().modifGraphElement(((GraphEdgeEditPart) editPart).getGraphEdge());
				} else {
					((GraphNodeEditPart) editPart).getModelManager().modifGraphElement(((GraphNodeEditPart) editPart).getGraphNode());
				}
			} 

			// Change graph node appearance
			if ( (notification.getEventType() ==  Notification.SET) && (notification.getNotifier() instanceof Property) ) {
				Property aProperty = (Property)notification.getNotifier();
				if (aProperty.getKey().equals(NodeHelper.NODE_FIGURE_APPEARANCE)){
					Debug.debug(this, "CHANGE " + NodeHelper.NODE_FIGURE_APPEARANCE, channel);		
					if( editPart instanceof GraphNodeEditPart){
						((GraphNodeEditPart) editPart).changeEditPartFigure();
					}		
				}		
			}

			// New extension point in a use case
//			if ( editPart instanceof ExtensionPointListEditPart 
//					&& (notification.getEventType() == Notification.ADD 
//							|| notification.getEventType() == Notification.REMOVE) ) {
//				Debug.debug(this, "MODIFY " + NodeHelper.NODE_FIGURE_APPEARANCE, channel);	
//				((UseCaseEditPart) editPart.getParent()).modifyEllipseFormAppearance();		
//			}		
		}
	}
	/**
	 * Gets the target.
	 * 
	 * @return the target
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return this._target;
	}

	/**
	 * Sets the target.
	 * 
	 * @param newTarget the new target
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		this._target = newTarget;
	}

	/**
	 * Checks if is adapter for type.
	 * 
	 * @param type the type
	 * 
	 * @return true, if is adapter for type
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return type.equals(editPart.getModel().getClass());
	}

}